<?php
require_once("vnpay/libs/nusoap.php");
require_once("config.php");
require_once("functions.php");

$namespace = "MOReceive";

$server = new soap_server();
$server->configureWSDL($namespace);
$server->wsdl->schemaTargetNamespace = $namespace;

$server->register(  'DeliverMO'
				  , array(  'seqno'          => 'xsd:int'
						  , 'commmethod'         => 'xsd:string'
						  , 'sendfrom'   => 'xsd:string'
                          , 'destination'         => 'xsd:string'
                          , 'keywordname'        => 'xsd:string'
                          , 'incontent'       => 'xsd:string')

				  , array('return'=>'xsd:string')
                  , $namespace
                  , false
                  , 'rpc'
                  , 'encoded'
                  , 'Receiving MO from SMS Gateway');
/* ý Nghĩa
seqno: Mã yêu cầu của khách hàng, số này dùng cho việc xác định MT phản hồi cho MO này.
CommMethod: Tên nhà cung cấp dịch vụ theo cấu hình của VNPAY. Các giá trị cho phép: Vinaphone, Mobifone, Viettel, EVNTel, SFone
SendFrom: Số dịch vụ (8x17)
Destination: Số di động gửi đến (Theo chuẩn international, bắt đầu bằng 84)
KeywordName: Mã của dịch vụ (Tùy theo từng dịch vụ), mã này sẽ phục vụ cho việc thống kê và quản lý MT phát sinh.
InContent: Nội dung tin nhắn (<= 160 ký tự)
*/
function DeliverMO($seqno, $commmethod, $sendfrom, $destination, $keywordname, $incontent)
{
	global $conf;
	$arrReturn = array('ReceiveResult' => 1,  'MessageResponse' => 'Cu phap tin nhan khong chinh xac');
	if($seqno != '' && $commmethod != '' && $sendfrom != '' && $destination != '' && $keywordname != '' && $incontent != ''){
		// save to DB
		$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
		mysql_select_db($conf['db_name']);
		$sqlInsert = "INSERT INTO tbl_sms_mo(mo_id, telco, service_num, phone, syntax, message_request, receive_date, status) VALUES
		('".intval($seqno)."', '".cleanValue($commmethod)."', '".cleanValue($sendfrom)."', '".cleanValue($destination)."', '".cleanValue($keywordname)."', '".cleanValue($incontent)."', NOW(), 1)";
		mysql_query($sqlInsert);
		if(preg_match('/'.cleanValue($keywordname).' (.*)/is', $incontent, $match)){
			$sms_code = $match[1];
			$tmpService = substr($sendfrom, 0, 2);
			$smsCost = 0;
			$smsFTP = 0;
			switch($tmpService){
				case "87":
					$smsCost = 15000;
					$smsVNPay = 6750;
				break;
				case "86":
					$smsCost = 10000;
					$smsVNPay = 4500;
				break;
				case "85":
					$smsCost = 5000;
					$smsVNPay = 2250;
				break;
				case "84":
					$smsCost = 4000;
					$smsVNPay = 1600;
				break;
				case "83":
					$smsCost = 3000;
					$smsVNPay = 1200;
				break;
				case "82":
					$smsCost = 2000;
					$smsVNPay = 800;
				break;
				case "81":
					$smsCost = 1000;
					$smsVNPay = 350;
				break;
				default:
					$smsCost = 500;
					$smsVNPay = 0;
				break;
			}
		
			$sqlStat = "INSERT INTO tbl_sms_stat(sms_syntax, sms_code, sms_cost, sms_vnpay, receive_date) VALUES('".$keywordname."', '".$sms_code."', $smsCost, $smsVNPay, NOW())";
			mysql_query($sqlStat);
		}
		mysql_close($con);
			
		$flag = "1";		
		$total = "1";
		$type = "0";
		$outcontent = "Chuc mung ban da kich hoat thanh cong tai khoan"  . strtoupper($keywordname) . ". Xem them video va Tai game hot mien phi tai http://imobiz.net/";
		$tmpService = substr($sendfrom, 0, 2);
		switch (strtoupper($keywordname))
			{
				case "DATN":
					$outcontent = "Dap an de thi tot nghiep THPT. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TN":
					$outcontent = "Diem thi tot nghiep THPT. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TBQ":
					$outcontent = "Bi quyet mua thi. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DIEM":
					$outcontent = "Diem thi vao lop 10. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DCTH":
					$outcontent = "Diem chuan lop 10. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TDD":
					$outcontent = "Dia diem thi DH-CD. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DA":
					$outcontent = "Dap an de thi DH-CD. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TD":
					$incontent = str_replace(".","",$incontent);
					$incontent = str_replace("-","",$incontent);
					if ($tmpService == 87){
						$outcontent = check_diemdh($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					}else{
						$outcontent = "Tin nhan thanh cong. Hay soan tin theo cu phap TD SoBaoDanh gui 8717. SoBaoDanh: MaTruong+KhoiThi+So. VD: TD BKAA12345 gui 8717";
					}
					break;
				case "DC":
					if ($tmpService >= 85){
						$outcontent = check_diemchuan($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					}else{
						$outcontent = "Tin nhan thanh cong. Hay soan tin theo cu phap DC MaTruong gui 8517 de nhan ket qua tra Diem Chuan cua cac truong DH-CD.";
					}
					break;
				case "TLC":
					if ($tmpService >= 85){
						$outcontent = check_tylechoi($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					}else{
						$outcontent = "Tin nhan thanh cong. Hay soan tin theo cu phap TLC MaTruong gui 8517 de nhan ket qua tra Ty Le Choi cua cac truong DH-CD.";
					}
					break;
				case "CT":
					if ($tmpService >= 85){
						$outcontent = check_chitieu($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					}else{
						$outcontent = "Tin nhan thanh cong. Hay soan tin theo cu phap CT MaTruong gui 8517 de nhan ket qua tra Chi Tieu cua cac truong DH-CD.";
					}
					break;
				case "TXH":
					if ($tmpService >= 85){
						$outcontent = check_xephang($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					}else{
						$outcontent = "Tin nhan thanh cong. Hay soan tin theo cu phap TXH SoBaoDanh gui 8517. SoBaoDanh: MaTruong+KhoiThi+So. VD: TXH BKAA12345 gui 8517";
					}
					break;
				case "XDT":
					if ($tmpService >= 85){
						$outcontent = check_dotruot($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					}else{
						$outcontent = "Tin nhan thanh cong. Hay soan tin theo cu phap XDT SoBaoDanh gui 8517. SoBaoDanh: MaTruong+KhoiThi+So. VD: XDT BKAA12345 gui 8517";
					}
					break;
				case "XMD":
					$outcontent = check_mucdiem($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					break;
				case "XTK":
					$outcontent = check_thukhoa($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					break;
				case "NVA":
					if ($tmpService >= 85){
						$outcontent = check_nguyenvong2($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					}else{
						$outcontent = "Tin nhan thanh cong. Hay soan tin theo cu phap NVA MaTruong gui 8517 de nhan ket qua tra Nguyen Vong 2 cua cac truong DH-CD.";
					}
					break;
				case "NVB":
					if ($tmpService >= 85){
						$outcontent = check_nguyenvong3($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					}else{
						$outcontent = "Tin nhan thanh cong. Hay soan tin theo cu phap NVB MaTruong gui 8517 de nhan ket qua tra Nguyen Vong 3 cua cac truong DH-CD.";
					}
					break;
				case "MD":
					$outcontent = "Xem so luong thi sinh cung muc diem. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "XD":
					$outcontent = "Xem tra diem thi DH. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DT":
					$incontent = str_replace(".","",$incontent);
					$incontent = str_replace("-","",$incontent);
					if (($tmpService == 87) && (strpos($incontent,".") == false) && (strpos($incontent,"-") == false)){
						$outcontent = check_diemdh($seqno, $commmethod, $sendfrom, $keywordname, $destination, $incontent);
					}else{
						$outcontent = "Tin nhan thanh cong. Hay soan tin theo cu phap TD SoBaoDanh gui 8717. SoBaoDanh: MaTruong+KhoiThi+So. VD: TD BKAA12345 gui 8717";
					}
					break;
				case "XDC":
					$outcontent = "Xem diem chuan truong DH. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "XH":
					$outcontent = "Xep hang thi sinh. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TDT":
					$outcontent = "Tra diem thi, thong tin tuyen sinh. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TDC":
					$outcontent = "Tra diem chuan. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DH":
					$outcontent = "Tra cuu DH. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "CD":
					$outcontent = "Tra cuu diem thi CD. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "XCT":
					$outcontent = "Xem chi tieu DH CD. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TCT":
					$outcontent = "Tra chi tieu tuyen sinh. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TTK":
					$outcontent = "Tra cuu thu khoa. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "XNA":
					$outcontent = "Tra cuu nguyen vong 2. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "XNB":
					$outcontent = "tra nguyen vong 3. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DCM":
					$outcontent = "Diem chuan lop 10. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DM":
					$outcontent = "Diem lop 10. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "MV":
					$outcontent = "Nhac chuong cho Mobile. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "CHOI":
					$outcontent = "Tra ty le choi DH CD. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DU":
					$outcontent = "Tra cuu diem thi DH CD. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TRA":
					$outcontent = "Dich vu tra cuu thong tin. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "KH":
					$outcontent = "Kich hoat KH. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TA":
					$outcontent = "Tai anh nen anh hot. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TG":
					$outcontent = "Tai Game hot. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "NAP":
					$outcontent = "Nap tien tai khoan. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TV":
					$outcontent = "Tu van khach hang. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "AU":
					$outcontent = "Thong tin gia Vang. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "CK":
					$outcontent = "Thong tin chung khoan. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "GH":
					$outcontent = "Gian hang thuong mai. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "XS":
					$outcontent = "Tin nhan thanh cong. Hien tai he thong dang nang cap du lieu Xo So Kien Thiet. Mong cac ban thong cam. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "BD":
					$outcontent = "Bong Da. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DOC":
					$outcontent = "Ung dung doc truyen. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "HOT":
					$outcontent = "Game, Anh, Nhac Hot. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "NHAC":
					$outcontent = "Nhac chuong dien thoai. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "BQ":
					$outcontent = "Bi quyet. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "CHO":
					$outcontent = "Nhac chuong cho dt. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "TR":
					$outcontent = "Ung dung doc truyen. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "GA":
					$outcontent = "50000:cong port mobile. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "HA":
					$outcontent = "Hinh anh hot. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "GT":
					$outcontent = "Dich vu gia tang thuong mai. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "NT":
					$outcontent = "Thong tin Ngoai Te. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DD":
					$outcontent = "Tra cuu dia diem. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "VT":
					$outcontent = "tra cuu vi tri. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "DK":
					$outcontent = "Dang ky dich vu. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "VIP":
					$outcontent = "Dich vu VIP. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
				case "KM":
					$outcontent = "Dich vu KM. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					break;
			}
		
		$result = SendMT($seqno, $sendfrom, $destination, $keywordname, $outcontent, $flag, $total, $type);		
		
		return $result;
		
	}
	return "-1|Loi Tin Nhan";
}

//XTK xem tra ve thu khoa truong
function check_thukhoa($moid, $telco, $service_num, $syntax, $phone, $message)
{
	$return = "Tin nhan thanh cong. Du lieu muc diem dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
	return $return;
}

//NVA xem tra ve Nguyen Vong 2
function check_nguyenvong2($moid, $telco, $service_num, $syntax, $phone, $message)
{
	global $conf;	
	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
	mysql_select_db($conf['db_name']);	
	
	$table = "tbl_nguyenvong2_mo";
	//chua co bang nguyen vong 2 truong dh				
	$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '" . strtolower($table) . "'";
	$qcheck=mysql_query($sqlcheck);
	$rcheck=mysql_fetch_array($qcheck);
	if ($rcheck[0] == 0) {
		//Tao bang du lieu luu tru cho cua truong dh
		$sqlcreate = "CREATE TABLE IF NOT EXISTS `" . strtolower($table) . "` (
						  `id` bigint(20) NOT NULL AUTO_INCREMENT,
						  `mo_id` bigint(20) NOT NULL,
						  `telco` varchar(255) NOT NULL,
						  `service_num` varchar(255) NOT NULL,
						  `syntax` varchar(255) NOT NULL,  
						  `phone` varchar(255) NOT NULL,  
						  `message` varchar(255) NOT NULL,
						  `receive_date` datetime NOT NULL,
						  `status` tinyint(4) NOT NULL DEFAULT '0',
						  PRIMARY KEY (`id`)
						) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
		$qcreate=mysql_query($sqlcreate);
	}
	$sqlInsert = "Insert Into " . $table . " (mo_id, telco, service_num, syntax, phone, message, receive_date, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
	
	mysql_query($sqlInsert);	
	mysql_close($con);
	$return = "Tin nhan thanh cong. Du lieu Nguyen Vong 2 dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
	return $return;
}

//NVB xem tra ve Nguyen Vong 3
function check_nguyenvong3($moid, $telco, $service_num, $syntax, $phone, $message)
{
	global $conf;	
	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
	mysql_select_db($conf['db_name']);	
	
	$table = "tbl_nguyenvong3_mo";
	//chua co bang nguyen vong 3 truong dh				
	$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '" . strtolower($table) . "'";
	$qcheck=mysql_query($sqlcheck);
	$rcheck=mysql_fetch_array($qcheck);
	if ($rcheck[0] == 0) {
		//Tao bang du lieu luu tru cho cua truong dh
		$sqlcreate = "CREATE TABLE IF NOT EXISTS `" . strtolower($table) . "` (
						  `id` bigint(20) NOT NULL AUTO_INCREMENT,
						  `mo_id` bigint(20) NOT NULL,
						  `telco` varchar(255) NOT NULL,
						  `service_num` varchar(255) NOT NULL,
						  `syntax` varchar(255) NOT NULL,  
						  `phone` varchar(255) NOT NULL,  
						  `message` varchar(255) NOT NULL,
						  `receive_date` datetime NOT NULL,
						  `status` tinyint(4) NOT NULL DEFAULT '0',
						  PRIMARY KEY (`id`)
						) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
		$qcreate=mysql_query($sqlcreate);
	}
	$sqlInsert = "Insert Into " . $table . " (mo_id, telco, service_num, syntax, phone, message, receive_date, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
	
	mysql_query($sqlInsert);	
	mysql_close($con);
	$return = "Tin nhan thanh cong. Du lieu Nguyen Vong 3 dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
	return $return;
}

//XMD xem tra ve do truot
function check_mucdiem($moid, $telco, $service_num, $syntax, $phone, $message)
{
	$return = "Tin nhan thanh cong. Du lieu muc diem dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
	return $return;
}

//XDT xem tra ve do truot
function check_dotruot($moid, $telco, $service_num, $syntax, $phone, $message)
{
	global $conf;	
	$cuphap = substr($message, 3, strlen($message) - 1);
	$cuphap = trim($cuphap);
	
	if(strpos($cuphap,"APP") !== false) {
		$cuphap = str_replace('APP','',$cuphap);
		$cuphap = trim($cuphap);
	}
	if(strpos($cuphap,"APA") !== false) {
		$cuphap = str_replace('APA','',$cuphap);
		$cuphap = trim($cuphap);
	}
	
	if (strpos($cuphap," ") == false){
		$cuphap = substr($cuphap, 0, 3) . " " . substr($cuphap, 3, 1) . " " . substr($cuphap, 4, strlen($cuphap) - 1);
	}else{
		$tach = explode(" ", $cuphap);				
		$cuphap = substr($tach[0], 0, 3) . " " . substr($tach[0], 3, strlen($tach[0]) -1) . " " . $tach[1];
	}
	//Tach Ma-Truong va So-Bao-Danh 
	$tach = explode(" ", $cuphap);
	$matruongdh = $tach[0];
	$khoi = $tach[1];
	$sbd = $tach[2];
	
	$return = "Tin nhan thanh cong. Soan tin TD " . $matruongdh . $khoi . $sbd .  " gui 8717 de nhan ket qua diem thi cua ban som nhat" . $matruongdh . ". Xem them tai http://kenh.xemtradiemthi.vn";
	//kiem tra du lieu diem thi cua truong dh da co chua
	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
	mysql_select_db($conf['db_name']);
	
	$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'dhcd_" . strtolower($matruongdh) . "'";
	$query=mysql_query($sqlcheck);
	$row = mysql_fetch_array($query);
	if ($row[0] == 0) {
		//chua co bang diem truong dh -> kiem tra bang luu cho cua truong dh		
		$return = "Tin nhan thanh cong. Soan tin TD " . $matruongdh . $khoi . $sbd .  " gui 8717 de nhan ket qua diem thi cua ban som nhat" . $matruongdh .$row[0]. ". Xem them tai http://kenh.xemtradiemthi.vn";
	}else{
		//Tra ket qua cho thi sinh
		$sqlSelect = "Select dhcd_dm1, dhcd_dm2, dhcd_dm3, dhcd_dtc0, dhcd_dtc From `dhcd_" . strtolower($matruongdh) . "` Where dhcd_sobaodanh = '" . trim($sbd) . "'";
		$query2 = mysql_query($sqlSelect);
		
		while($row=mysql_fetch_array($query2))	
		{	
			$return = "Diem thi cua ban: M1: ".$row['dhcd_dm1'].", M2: ".$row['dhcd_dm2'].", M3: ".$row['dhcd_dm3'].". Tong: " . $row['dhcd_dtc'];
			
			//Xet thi sinh = diem
			$sql = " SELECT	count(dhcd_id) as t1, sum(if(dhcd_khuvuc='1', 1,0)) as t2, sum(if(dhcd_khuvuc='2', 1,0)) as t3, sum(if(dhcd_khuvuc='2NT', 1,0)) as t4, sum(if(dhcd_khuvuc='3', 1,0)) as t5	FROM dhcd_" . strtolower($matruongdh) . "	WHERE dhcd_dtc0='".$row['dhcd_dtc0']."';";
			$equar = mysql_query($sql);
			while($row2=mysql_fetch_array($equar))	
			{
				$return = $return . " - So thi sinh = diem: " . $row2['t1'];
			}
			
			//Xet thi sinh < diem
			$sql = " SELECT	count(dhcd_id) as t1, sum(if(dhcd_khuvuc='1', 1,0)) as t2, sum(if(dhcd_khuvuc='2', 1,0)) as t3, sum(if(dhcd_khuvuc='2NT', 1,0)) as t4, sum(if(dhcd_khuvuc='3', 1,0)) as t5	FROM dhcd_" . strtolower($matruongdh) . "	WHERE dhcd_dtc0<'".$row['dhcd_dtc0']."';";
			$equar = mysql_query($sql);
				
			while($row2=mysql_fetch_array($equar))	
			{
				$return = $return . " - So thi sinh < diem: " . $row2['t1'];
			}
			
			//Xet thi sinh < diem
			$sql = " SELECT	count(dhcd_id) as t1, sum(if(dhcd_khuvuc='1', 1,0)) as t2, sum(if(dhcd_khuvuc='2', 1,0)) as t3, sum(if(dhcd_khuvuc='2NT', 1,0)) as t4, sum(if(dhcd_khuvuc='3', 1,0)) as t5	FROM dhcd_" . strtolower($matruongdh) . "	WHERE dhcd_dtc0>'".$row['dhcd_dtc0']."';";
			$equar = mysql_query($sql);
				
			while($row2=mysql_fetch_array($equar))	
			{
				$return = $return . " - So thi sinh > diem: " . $row2['t1'];
			}
			$return = $return . ". Xem them tai http://kenh.xemtradiemthi.vn";
		}
	}
	
	mysql_close($con);
	return $return;
}

//TXH xem tra ve xep hang 
function check_xephang($moid, $telco, $service_num, $syntax, $phone, $message)
{
	$return = "Tin nhan thanh cong. Du lieu xep hang dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
	return $return;
}

//CT xem tra ve chi tieu dai hoc
function check_chitieu($moid, $telco, $service_num, $syntax, $phone, $message)
{
	global $conf;	
	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
	mysql_select_db($conf['db_name']);	
	
	$table = "tbl_chitieu_mo";
	//chua co bang ty le choi truong dh				
	$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '" . strtolower($table) . "'";
	$qcheck=mysql_query($sqlcheck);
	$rcheck=mysql_fetch_array($qcheck);
	if ($rcheck[0] == 0) {
		//Tao bang du lieu luu tru cho cua truong dh
		$sqlcreate = "CREATE TABLE IF NOT EXISTS `" . strtolower($table) . "` (
						  `id` bigint(20) NOT NULL AUTO_INCREMENT,
						  `mo_id` bigint(20) NOT NULL,
						  `telco` varchar(255) NOT NULL,
						  `service_num` varchar(255) NOT NULL,
						  `syntax` varchar(255) NOT NULL,  
						  `phone` varchar(255) NOT NULL,  
						  `message` varchar(255) NOT NULL,
						  `receive_date` datetime NOT NULL,
						  `status` tinyint(4) NOT NULL DEFAULT '0',
						  PRIMARY KEY (`id`)
						) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
		$qcreate=mysql_query($sqlcreate);
	}
	$sqlInsert = "Insert Into " . $table . " (mo_id, telco, service_num, syntax, phone, message, receive_date, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
	
	mysql_query($sqlInsert);	
	mysql_close($con);
	
	$return = "Tin nhan thanh cong. Du lieu chi tieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
	return $return;
}

//TLC xem tra ve ty le choi 
function check_tylechoi($moid, $telco, $service_num, $syntax, $phone, $message)
{
	global $conf;	
	$cuphap = substr($message, 3, strlen($message) - 1);
	$cuphap = trim($cuphap);
	$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
	
	//XDC Tra ve ket qua diem chuan
	if(strpos($cuphap,"app") !== false) {
		$cuphap = str_replace('app','',$cuphap);
		$cuphap = trim($cuphap);
	}
	if(strpos($cuphap,"apa") !== false) {
		$cuphap = str_replace(' apa','',$cuphap);
		$cuphap = trim($cuphap);
	}
	$sqlSelect = "Select tieude, noidung, link, status From tbl_tilechoi Where CuPhap = '" . strtoupper($cuphap) . "'";
	$table = "tbl_tilechoi_mo";
	
	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
	mysql_select_db($conf['db_name']);	
	$query=mysql_query($sqlSelect);
	
	if(mysql_num_rows($query)  != 0)
	{	
		while($row=mysql_fetch_array($query))	
		{	
			$return = $row['noidung'] . "... Xem them tai " . $row['link'];	
			//Kiem tra neu chua co status = 0 thi se insert vao bang tuong ung de gui tin nhan khi trang thai duoc cap nhat status = 1
			if ($row['status'] == 0 ){
				//chua co bang ty le choi truong dh				
				$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '" . strtolower($table) . "'";
				$qcheck=mysql_query($sqlcheck);
				$rcheck=mysql_fetch_array($qcheck);
				if ($rcheck[0] == 0) {
					//Tao bang du lieu luu tru cho cua truong dh
					$sqlcreate = "CREATE TABLE IF NOT EXISTS `" . strtolower($table) . "` (
									  `id` bigint(20) NOT NULL AUTO_INCREMENT,
									  `mo_id` bigint(20) NOT NULL,
									  `telco` varchar(255) NOT NULL,
									  `service_num` varchar(255) NOT NULL,
									  `syntax` varchar(255) NOT NULL,  
									  `phone` varchar(255) NOT NULL,  
									  `message` varchar(255) NOT NULL,
									  `receive_date` datetime NOT NULL,
									  `status` tinyint(4) NOT NULL DEFAULT '0',
									  PRIMARY KEY (`id`)
									) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
					$qcreate=mysql_query($sqlcreate);
				}
				$sqlInsert = "Insert Into " . $table . " (mo_id, telco, service_num, syntax, phone, message, receive_date, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
			}
		}
	}else{
		$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES
	('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
	}	
	
	mysql_query($sqlInsert);
	
	mysql_close($con);
	return $return;
}

//XDC xem tra ve diem chuan
function check_diemchuan($moid, $telco, $service_num, $syntax, $phone, $message)
{
	global $conf;	
	$cuphap = substr($message, 2, strlen($message) - 1);
	$cuphap = trim($cuphap);
	$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
	
	//XDC Tra ve ket qua diem chuan
	if(strpos($cuphap,"app") !== false) {
		$cuphap = str_replace('app','',$cuphap);
		$cuphap = trim($cuphap);
	}
	if(strpos($cuphap,"apa") !== false) {
		$cuphap = str_replace(' apa','',$cuphap);
		$cuphap = trim($cuphap);
	}
	$sqlSelect = "Select tieude, noidung, link, status From tbl_diemchuan Where CuPhap = '" . strtoupper($cuphap) . "'";
	//$return = "Ban da gui tin nhan thanh cong. Xem them thong tin mua thi tai http://kenh.xemtradiemthi.vn/";
	$table = "tbl_diemchuan_mo";
	
	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
	mysql_select_db($conf['db_name']);	
	$query=mysql_query($sqlSelect);
	
	if(mysql_num_rows($query)  != 0)
	{	
		while($row=mysql_fetch_array($query))	
		{	
			$return = $row['noidung'] . "... Xem them tai " . $row['link'];	
			//Kiem tra neu chua co status = 0 thi se insert vao bang tuong ung de gui tin nhan khi trang thai duoc cap nhat status = 1
			if ($row['status'] == 0 ){
				//chua co bang diem chuan truong dh
				$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '" . strtolower($table) . "'";
				$qcheck=mysql_query($sqlcheck);
				$rcheck=mysql_fetch_array($qcheck);
				if ($rcheck[0] == 0) {
					//Tao bang du lieu luu tru cho cua truong dh
					$sqlcreate = "CREATE TABLE IF NOT EXISTS `" . strtolower($table) . "` (
									  `id` bigint(20) NOT NULL AUTO_INCREMENT,
									  `mo_id` bigint(20) NOT NULL,
									  `telco` varchar(255) NOT NULL,
									  `service_num` varchar(255) NOT NULL,
									  `syntax` varchar(255) NOT NULL,  
									  `phone` varchar(255) NOT NULL,  
									  `message` varchar(255) NOT NULL,
									  `receive_date` datetime NOT NULL,
									  `status` tinyint(4) NOT NULL DEFAULT '0',
									  PRIMARY KEY (`id`)
									) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
					$qcreate=mysql_query($sqlcreate);
				}
				$sqlInsert = "Insert Into " . $table . " (mo_id, telco, service_num, syntax, phone, message, receive_date, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
			}
		}
	}else{
		$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES
	('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
	}	
	
	mysql_query($sqlInsert);
	
	mysql_close($con);
	return $return;
}

//TD Diem thi Dai Hoc Cao Dang - cac dich vu cua cu phap TD, XD
//Chua du du lieu de lam 
function check_diemdh($moid, $telco, $service_num, $syntax, $phone, $message)
{		
		global $conf;	
		$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
		mysql_select_db($conf['db_name']);
		
		if(strpos($cuphap,"APP") !== false) {
			$cuphap = str_replace('APP','',$cuphap);
			$cuphap = trim($cuphap);
		}
		if(strpos($cuphap,"APA") !== false) {
			$cuphap = str_replace('APA','',$cuphap);
			$cuphap = trim($cuphap);
		}
		$cuphap = substr($message, 2, strlen($message) - 1);
		$cuphap = trim($cuphap);
		$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
		$b = 1;
		
		if(strpos($cuphap,"KT") !== false) {
			//Tra ket qua ve diem kiem tra lai cua thi sinh
			$cuphap = substr($cuphap, 2, strlen($cuphap) - 1);
			$cuphap = trim($cuphap);
			if (strpos($cuphap," ") == false){
			 	$cuphap = substr($cuphap, 0, 3) . " " . substr($cuphap, 3, 1) . " " . substr($cuphap, 4, strlen($cuphap) - 1);
			}else{
				$tach = explode(" ", $cuphap);				
				$cuphap = substr($tach[0], 0, 3) . " " . substr($tach[0], 3, strlen($tach[0]) -1) . " " . $tach[1];
			}
			$tach = explode(" ", $cuphap);
			$matruongdh = $tach[0];
			$khoi = $tach[1];
			$sbd = $tach[2];		
			$b = 0;		
		}else {
			if (strpos($cuphap," ") == false){
				$cuphap = substr($cuphap, 0, 3) . " " . substr($cuphap, 3, 1) . " " . substr($cuphap, 4, strlen($cuphap) - 1);
			}else{
				$tach = explode(" ", $cuphap);				
				$cuphap = substr($tach[0], 0, 3) . " " . substr($tach[0], 3, strlen($tach[0]) -1) . " " . $tach[1];
			}
			//Tach Ma-Truong va So-Bao-Danh 
			$tach = explode(" ", $cuphap);
			$matruongdh = $tach[0];
			$khoi = $tach[1];
			$sbd = $tach[2];						
		}
		
		//kiem tra du lieu diem thi cua truong dh da co chua
		$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'dhcd_" . strtolower($matruongdh) . "'";
		$query=mysql_query($sqlcheck);
		$row=mysql_fetch_array($query);
		if ($row[0] == 0) {
			//chua co bang diem truong dh -> kiem tra bang luu cho cua truong dh
			$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'dhcd" . strtolower($matruongdh) . "_mo'";
			$qcheck=mysql_query($sqlcheck);
			$rcheck=mysql_fetch_array($qcheck);
			if ($rcheck[0] == 0) {
				//Tao bang du lieu luu tru cho cua truong dh
				$sqlcreate = "CREATE TABLE IF NOT EXISTS `dhcd_" . strtolower($matruongdh) . "_mo` (
								  `id` bigint(20) NOT NULL AUTO_INCREMENT,
								  `mo_id` bigint(20) NOT NULL,
								  `telco` varchar(255) NOT NULL,
								  `service_num` varchar(255) NOT NULL,
								  `syntax` varchar(255) NOT NULL,  
								  `phone` varchar(255) NOT NULL,  
								  `message` varchar(255) NOT NULL,
								  `receive_date` datetime NOT NULL,
								  `sobaodanh` varchar(255) NOT NULL,
								  `status` tinyint(4) NOT NULL DEFAULT '0',
								  PRIMARY KEY (`id`)
								) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
				$qcreate=mysql_query($sqlcreate);
			}
			//Chen du lieu thi sinh vao bang luu tru cho cua truong dh
			$sqlInsert = "Insert Into `dhcd_" . strtolower($matruongdh) . "_mo`(mo_id, telco, service_num, syntax, phone, message, receive_date, sobaodanh, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), '" .cleanValue($sbd)."', 0)";
			mysql_query($sqlInsert);
			
			if ($b == 0){ $return = "Tin nhan thanh cong! Hay kiem tra lai cu phap nhan tin de nhan ket qua diem thi cua ban. Soan TD SBD gui 8717";}
			else{
				$return = "Tin nhan thanh cong. Du lieu cua truong " . $matruongdh . " dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
			}
		}else{
			//Tra ket qua cho thi sinh
			$sqlSelect = "Select dhcd_dm1, dhcd_dm2, dhcd_dm3 From `dhcd_" . strtolower($matruongdh) . "` Where dhcd_sobaodanh = '" . trim($sbd) . "'";
			$query=mysql_query($sqlSelect);
	
			if(mysql_num_rows($query)  != 0)
			{	
				while($row=mysql_fetch_array($query))	
				{	
					$return = "Diem thi cua ban: M1: ".$row['dhcd_dm1'].", M2: ".$row['dhcd_dm2'].", M3: ".$row['dhcd_dm3'].". Soan tin DC " . $matruongdh. " gui 8517 de nhan diem chuan som nhat. Xem them http://kenh.xemtradiemthi.vn";
					if ($b == 0){$return = "Diem thi cua ban: M1: ".$row['dhcd_dm1'].", M2: ".$row['dhcd_dm2'].", M3: ".$row['dhcd_dm3'].". Hay vao http://kenh.xemtradiemthi.vn/tuyensinh/box_tra_diem_thi/ de biet chi tiet";}
				}
			}else{
				$return = "Soan tin TD SBD gui 8717. Trong do SBD: Matruong+KhoiThi+So; doi voi khoi thi A1, D1.. SBD: Matruong+Khoithi So. VD: TD BKAA1234 gui 8717";
			}
		}
		
		mysql_close($con);
		return $return;
}

function SendMT($seqno, $sendfrom, $destination, $keywordname, $outcontent, $flag = 1, $total = 1, $type = 0){
	
		$client = new nusoap_client('http://112.78.7.141:1500/MVASCONTENT/MTSend.asmx?WSDL', true);
	       $err = $client->getError();
	       if ($err) {
		      // Display the error
		      echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
		      exit;
		      // At this point, you know the call that follows will fail
	       }

              //$flag = "1";		
		//$total = "1";
		//$type = "0"; //4 port

              $client->soap_defencoding = 'UTF-8';
	       $secretcode = "123456";
	       $param = array (
			"Destination" => $destination, 
			"SendFrom" => $sendfrom,
			"KeywordName" => $keywordname,
			"OutContent" => $outcontent,
			"ChargingFlag" => $flag,
			"MOSeqNo" => $seqno,
			"TotalMessage" => $total,
			"ContentType" => $type,
			"SecretCode" => $secretcode,
		);


		try {
	       	$result = $client->call('SendMT', $param); 
		} catch (Exception $e) {
			$result =  'Caught exception: ' . $e->getMessage() . "====";
		}
		//ghi log 
		global $conf;
		$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
		mysql_select_db($conf['db_name']);
		$sqlInsert = "INSERT INTO log_pay(segno, telco, service_num, phone, syntax, message_return, log, result) VALUES
		('".intval($seqno)."', '".cleanValue($commmethod)."', '".cleanValue($sendfrom)."', '".cleanValue($destination)."', '".cleanValue($keywordname)."', '".cleanValue($outcontent)."', '". print_r($param,true) ."','". $result['SendMTResult'] ."')";
		mysql_query($sqlInsert);
		mysql_close($con);
	if ($result['SendMTResult'] == -1){
		return -1;
	}else{
		return 1;
	}
}
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

$server->service($POST_DATA);

exit();
?>